home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / libs / cwrlib31.lha / CWTerm / comserial.c next >
C/C++ Source or Header  |  1992-07-22  |  6KB  |  227 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/io.h>
  4. #include <exec/ports.h>
  5. #include <exec/memory.h>
  6. #include <hardware/cia.h>
  7. #include <devices/serial.h>
  8.  
  9. struct    IOExtSer *Read_Request;
  10. struct    MsgPort *ReadPort;
  11. extern char read_in[];
  12. struct    IOExtSer *Write_Request;
  13. struct    MsgPort *WritePort;
  14. char    rs_out[2];
  15. struct    IOExtSer *Echo_Request;
  16. struct    MsgPort *EchoPort;
  17. char    rs_echo[2];
  18.  
  19. int        Ser_Stat;
  20.  
  21. int ComOpen(
  22. char    *device,
  23. int        unit,
  24. long    baudrate,
  25. int        bits,
  26. int        parity,
  27. int        stopbits,
  28. int        WIRE7)
  29. {
  30.     if(ReadPort = (struct MsgPort*)CreatePort(0,0))
  31.     {
  32.         if(Read_Request = (struct IOExtSer*)
  33.             CreateExtIO(ReadPort,sizeof(struct IOExtSer)))
  34.         {
  35.             Read_Request->IOSer.io_Message.mn_ReplyPort = ReadPort;
  36.             Read_Request->io_SerFlags = SERF_SHARED|SERF_XDISABLED;
  37.             if(WIRE7)
  38.                 Read_Request->io_SerFlags |= SERF_7WIRE;
  39.             if((OpenDevice(device,unit,Read_Request,0)) == 0)
  40.             {
  41.                 Read_Request->IOSer.io_Command = CMD_READ;
  42.                 Read_Request->IOSer.io_Length = 1;
  43.                 Read_Request->IOSer.io_Data = (APTR)&read_in[0];
  44.                 if(WritePort = (struct MsgPort*)CreatePort(0,0))
  45.                 {
  46.                     if(Write_Request = (struct IOExtSer*)
  47.                         CreateExtIO(ReadPort,sizeof(struct IOExtSer)))
  48.                     {
  49.                         Write_Request->IOSer.io_Message.mn_ReplyPort = WritePort;
  50.                         Write_Request->io_SerFlags = SERF_SHARED|SERF_XDISABLED;
  51.                         if(WIRE7)
  52.                             Write_Request->io_SerFlags |= SERF_7WIRE;
  53.                         if((OpenDevice(device,unit,Write_Request,0)) == 0)
  54.                         {
  55.                             Write_Request->IOSer.io_Command = SDCMD_QUERY;
  56.                             DoIO(Write_Request);
  57.                             Write_Request->IOSer.io_Command    = CMD_WRITE;
  58.                             Write_Request->IOSer.io_Length    = 1;
  59.                             Write_Request->IOSer.io_Data    = (APTR)&rs_out[0];
  60.                             if(EchoPort = (struct MsgPort*)CreatePort(0,0))
  61.                             {
  62.                                 if(Echo_Request = (struct IOExtSer*)
  63.                                     CreateExtIO(EchoPort,sizeof(struct IOExtSer)))
  64.                                 {
  65.                                     Echo_Request->IOSer.io_Message.mn_ReplyPort = EchoPort;
  66.                                     Echo_Request->io_SerFlags = SERF_SHARED|SERF_XDISABLED;
  67.                                     if(WIRE7)
  68.                                         Echo_Request->io_SerFlags |= SERF_7WIRE;
  69.                                     if((OpenDevice(device,unit,Echo_Request,0)) == 0)
  70.                                     {
  71.                                         Echo_Request->IOSer.io_Command = SDCMD_QUERY;
  72.                                         DoIO(Echo_Request);
  73.                                         Echo_Request->IOSer.io_Command    = CMD_WRITE;
  74.                                         Echo_Request->IOSer.io_Length    = 1;
  75.                                         Echo_Request->IOSer.io_Data        = (APTR)&rs_echo[0];
  76.  
  77.                                         Read_Request->io_SerFlags         = SERF_SHARED | SERF_XDISABLED;
  78.                                         if(WIRE7)
  79.                                         Read_Request->io_SerFlags |= SERF_7WIRE;
  80.                                         Read_Request->IOSer.io_Length    = 1;
  81.                                         Read_Request->IOSer.io_Data        = (APTR)&read_in[0];
  82.                                         Read_Request->io_Baud             = baudrate;
  83.                                         Read_Request->io_StopBits         = stopbits;
  84.                                         Read_Request->io_ReadLen          = bits;
  85.                                         Read_Request->io_WriteLen         = bits;
  86.                                         Read_Request->io_CtlChar          = 1L;
  87.                                         Read_Request->io_RBufLen          = 2048;
  88.                                         Read_Request->IOSer.io_Command    = SDCMD_SETPARAMS;
  89.                                         DoIO(Read_Request);
  90.                                         Read_Request->IOSer.io_Command    = CMD_READ;
  91.                                         return(TRUE);
  92.                                     }
  93.                                     DeleteExtIO(Echo_Request);
  94.                                 }
  95.                                 DeletePort(EchoPort);
  96.                             }
  97.                             CloseDevice(Write_Request);
  98.                         }
  99.                         DeleteExtIO(Write_Request);
  100.                     }
  101.                     DeletePort(WritePort);
  102.                 }
  103.                 CloseDevice(Read_Request);
  104.             }
  105.             DeleteExtIO(Read_Request);
  106.         }
  107.         DeletePort(ReadPort);
  108.     }
  109. return(FALSE);
  110. }
  111.  
  112. void ComClose()
  113. {
  114.     AbortIO(Read_Request);
  115.     WaitIO(Read_Request);
  116.     CloseDevice(Read_Request);
  117.     DeleteExtIO(Read_Request);
  118.     DeletePort(ReadPort);
  119.     CloseDevice(Write_Request);
  120.     DeleteExtIO(Write_Request);
  121.     DeletePort(WritePort);
  122.     CloseDevice(Echo_Request);
  123.     DeleteExtIO(Echo_Request);
  124.     DeletePort(EchoPort);
  125. }
  126.  
  127. int ComCarrier()
  128. {
  129.     int status;
  130.  
  131.     Echo_Request->IOSer.io_Command = SDCMD_QUERY;
  132.     DoIO(Echo_Request);
  133.     status = Echo_Request->io_Status & CIAF_COMCD;
  134.     return(status?FALSE:TRUE);
  135. }
  136.  
  137. void ComSetBaud(baudrate)
  138. long baudrate;
  139. {
  140.     if(!(CheckIO(Read_Request)))
  141.     {
  142.         AbortIO(Read_Request);
  143.         WaitIO(Read_Request);
  144.     }
  145.     Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  146.     Read_Request->io_Baud = baudrate;
  147.     DoIO(Read_Request);
  148.  
  149.     Read_Request->IOSer.io_Command = CMD_READ;    /* start reading again */
  150.     Read_Request->IOSer.io_Length = 1;
  151.     Read_Request->IOSer.io_Data = (APTR) &read_in[0];
  152. }
  153.  
  154. int ComSetFlow(flow)
  155. int flow;
  156. {
  157.     if(!(CheckIO(Read_Request)))
  158.     {
  159.         AbortIO(Read_Request);
  160.         WaitIO(Read_Request);
  161.     }
  162.     Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  163.     if(flow)
  164.         Read_Request->io_SerFlags |= SERF_7WIRE;
  165.     else
  166.         Read_Request->io_SerFlags &= ~SERF_7WIRE;
  167.     DoIO(Read_Request);
  168.  
  169.     Read_Request->IOSer.io_Command = CMD_READ;    /* start reading again */
  170.     Read_Request->IOSer.io_Length = 1;
  171.     Read_Request->IOSer.io_Data = (APTR) &read_in[0];
  172.     return(0);
  173. }
  174.  
  175. void ComPutc(ch)
  176. unsigned char ch;
  177. {
  178.     if(!(CheckIO(Write_Request)))
  179.         WaitIO(Write_Request);
  180.     rs_out[0] = ch & 0xFF;
  181.     Write_Request->IOSer.io_Command = CMD_WRITE;
  182.     Write_Request->IOSer.io_Length = 1;
  183.     Write_Request->IOSer.io_Data = (APTR)&rs_out[0];
  184.     SendIO(Write_Request);
  185. }
  186.  
  187. int ComRead(buffer,amount)
  188. unsigned char *buffer;
  189. int amount;
  190. {
  191.     int serlen;
  192.  
  193.     if(!(CheckIO(Read_Request)))
  194.     {
  195.         AbortIO(Read_Request);
  196.         WaitIO(Read_Request);
  197.     }
  198.  
  199.     Echo_Request->IOSer.io_Command = SDCMD_QUERY;
  200.     DoIO(Echo_Request);
  201.  
  202.     serlen = Echo_Request->IOSer.io_Actual;    /* How many bytes in RX buffer */
  203.  
  204.     if(serlen == 0)
  205.         return(0);
  206.     if(serlen > amount) serlen = amount;
  207.     Read_Request->IOSer.io_Length = serlen;
  208.     Read_Request->IOSer.io_Data = (APTR)buffer;
  209.     DoIO(Read_Request);
  210.     Read_Request->IOSer.io_Length = 1;
  211.     Read_Request->IOSer.io_Data = (APTR) &read_in[0];
  212.  
  213.     return(serlen);
  214. }
  215.  
  216. void ComWrite(buffer,amount)
  217. unsigned char *buffer;
  218. int amount;
  219. {
  220.     if(!(CheckIO(Write_Request)))
  221.         WaitIO(Write_Request);
  222.     Write_Request->IOSer.io_Command = CMD_WRITE;
  223.     Write_Request->IOSer.io_Length = amount;
  224.     Write_Request->IOSer.io_Data = (APTR)buffer;
  225.     DoIO(Write_Request);
  226. }
  227.